<template>
  <div id="stackBar">
    <div ref="chart01" class="chart01"></div>
  </div>
</template>

<script>
export default {
  name: 'stackBar',
  components: {},
  props: {
    xData: Array | Object,
    y1Data: Array | Object,
    series1: Array | Object,
    series2: Array | Object,
    series3: Array | Object,
    series4: Array | Object
  },
  data() {
    return {};
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.drawStackBar();
  },

  methods: {
    drawStackBar() {
      let that = this;
      let mychart = that.$echarts.init(that.$refs.chart01);
      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: 'line', // 默认为直线，可选为：'line' | 'shadow'
            lineStyle: {
              color: 'e5e5e5'
            }
          },
          formatter: function (params) {
            let resultName1 = '';
            let resultValue1 = '';
            let resultName2 = '';
            let resultValue2 = '';
            let result01 = '';
            params.forEach(function (item) {
              if (item.seriesName == '商品总数') {
                resultName1 = item.marker + ' ' + item.seriesName;
                resultValue1 = that.format_num(item.value);
              } else if (item.seriesName == '动销商品数') {
                resultName2 = item.marker + ' ' + item.seriesName;
                resultValue2 = that.format_num(item.value);
              } else {
                result01 +=
                  item.marker +
                  ' ' +
                  item.seriesName +
                  ' : ' +
                  that.format_num(item.value) +
                  '</br>';
              }
            });
            let result;
            if (resultValue1 && resultValue2) {
              result =
                params[0].name +
                '</br>' +
                resultName1 +
                '&nbsp ' +
                resultName2 +
                '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp' +
                resultValue1 +
                '/' +
                resultValue2 +
                '</br>' +
                // resultName3 +
                // "&nbsp " +
                // resultName4 +
                // "&nbsp&nbsp&nbsp&nbsp" +
                // resultValue3 +
                // "/" +
                // resultValue4 +
                // "</br>" +
                result01;
            } else if (!resultValue1) {
              result =
                params[0].name + '</br>' + resultName2 + ' : ' + resultValue2 + '</br>' + result01;
            } else if (!resultValue2) {
              result =
                params[0].name + '</br>' + resultName1 + ' : ' + resultValue1 + '</br>' + result01;
            }
            return result;
          }
        },
        legend: {
          //   data: [
          //     "商品总数",
          //     "动销商品数",
          //     "直播销量",
          //     "直播销售额",
          //   ],
          top: 28,
          itemWidth: 10,
          itemHeight: 10,
          itemGap: 30,
          textStyle: {
            color: '#555'
          }
        },
        grid: {
          left: '64',
          right: '74',
          bottom: '110',
          containLabel: false
        },
        xAxis: [
          {
            type: 'category',
            data: that.xData,
            //设置网格线颜色
            splitLine: {
              show: false,
              lineStyle: {
                color: ['#e5e5e5'],
                width: 1,
                type: 'dashed'
              }
            },
            // x轴样式
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#e5e5e5', //左边线的颜色
                width: '1' //坐标线的宽度
              }
            },
            axisTick: {
              show: false
            },
            // x轴字体样式
            axisLabel: {
              interval: 0,
              // rotate: 40,
              margin: 14,
              show: true,
              textStyle: {
                color: '#888',
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12
              }
            }
          }
        ],
        yAxis: [
          {
            name: '商品总数/动销商品数',
            type: 'value',
            //设置网格线颜色
            nameTextStyle: {
              color: '#888',
              padding: [0, 0, 0, 110]
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#e5e5e5'],
                width: 1,
                type: 'dashed'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#888',
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12
              },
              formatter: function (value) {
                return that.format_num(value);
              }
            },
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#e5e5e5', //左边线的颜色
                width: '1' //坐标线的宽度
              }
            }
          },
          {
            type: 'value',
            //设置网格线颜色
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#e5e5e5'],
                width: 1,
                type: 'dashed'
              }
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#888',
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12
              },
              formatter: function (value) {
                return that.format_num(value);
              }
            },
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#e5e5e5', //左边线的颜色
                width: '1' //坐标线的宽度
              }
            }
          }
        ],
        series: [
          {
            name: '商品总数',
            type: 'bar',
            // stack: "商品数",
            barGap: '-100%' /*柱子重叠*/,
            yAxisIndex: 0,
            data: that.series1,
            itemStyle: {
              normal: {
                // color: 'bad7ff',
                color: '#f4b477'
                //柱子颜色
              }
            },
            barWidth: 14 // 控制柱子的宽度
            // barGap: "1.5px" /*多个并排柱子设置柱子之间的间距*/,
            // barCategoryGap: "5px" /*多个并排柱子设置柱子之间的间距*/,
          },
          {
            name: '动销商品数',
            type: 'bar',
            // stack: "商品数",
            barGap: '-100%' /*柱子重叠*/,
            yAxisIndex: 0,
            data: that.series2,
            itemStyle: {
              normal: {
                // color: "#1779ff",
                color: '#f0935e'
                //柱子颜色
              }
            },
            barWidth: 14 // 控制柱子的宽度
            // barGap: "1.5px" /*多个并排柱子设置柱子之间的间距*/,
          }
        ]
      };
      mychart.setOption(option);
      //   窗口自适应
      setTimeout(() => {
        let resizeFn = function () {
          mychart.resize();
        };
        window.addEventListener('resize', resizeFn);
        this.$once('hook:beforeDestroy', () => {
          mychart.dispose();
          mychart = null;
          window.removeEventListener('resize', resizeFn);
        });
      }, 200);
    }
  }
};
</script>
<style lang='less' scoped>
#stackBar {
  width: 100%;
  height: 100%;
  margin-top: 36px;
  .chart01 {
    width: 100%;
    height: 100%;
    overflow: hidden;
    clear: both;
  }
}
</style>